import './axios.js';
import './PrototypeExtend.js';
import './ElementUI.js';
import './formatTime';
import './store.js';
import '@/assets/scss/app.scss';

import Vue from 'vue';
import VueI18n from 'vue-i18n';
import VueQuillEditor from 'vue-quill-editor';
import RouterMenusFilter from '@/helpers/RouterMenusFilter.js';
import store from '../store';
import router from '../router';

import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

Vue.use(VueQuillEditor);

Vue.use(VueI18n);

router.beforeEach(async (to, from, next) => {
    const checkRoute = from.path === '/' && !from.name && to.path !== '/login';

    if (to.path.indexOf('/helps') === -1 && to.path.indexOf('/course-selection') === -1) {
        // 非帮助中心、非选课中心下的路由
        if (checkRoute && !store.state.my) {
            await store.dispatch('getMy');
            if (!store.state.my) return next('/login');
        }
        // 帮助中心、选课中心下的路由
    } else if (!store.state.my) {
        const my = JSON.parse(sessionStorage.getItem('my/profile'));
        if (my && Object.keys(store).length) {
            store.commit('setMy', my);
        }
    }

    const myData = {...store.state.my};
    if (myData.permissions) myData.permission = myData.permissions;
    store.commit('setMy', myData);

    const menusFilter = (new RouterMenusFilter(store.state.my, router.options.routes));
    const url = menusFilter.notAccess(to);
    if (url === 'notify') {
        $ele.$message('进阶功能联系我们单独开通');
        return next(menusFilter.menus()[0]);
    }
    if (url) return next(url);

    next();
});

// router.afterEach((to, from) => {
//     if (to.name === from.name) return;

//     window.scrollTo(0, 1);
// });

const Api = require('../api').default;

window.API = new Api(process.env.VUE_APP_API_PREFIX);

Vue.directive('baguettebox', require('../directives/baguettebox').default);

const messages = {
    En: require('@/lang/en.json'),
    Ja: require('@/lang/ja.json'),
};

const i18n = new VueI18n({
    locale: 'En', // set locale
    messages, // set locale messages
    fallbackLocale: 'En',
});

export default {
    i18n,
    store,
    router,
};
